package com.cuit.bysj.action;

import java.util.List;

import com.cuit.bysj.entities.Appraise;
import com.cuit.bysj.entities.Item;
import com.cuit.bysj.entities.Student;
import com.cuit.bysj.entities.Teacher;
import com.cuit.bysj.entities.User;
import com.cuit.bysj.service.AppraiseTcherService;
import com.cuit.bysj.utils.JsonHelper;
import com.cuit.bysj.utils.Result;
import com.cuit.bysj.utils.SelectUtils;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class AppraiseTcherAction extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private String result;
	private AppraiseTcherService appraisetcherService;
	private String tid; // 教师id
	private String sid; // 课程id
	private String content; // 前台学生的评价
	private String selectArr; // 前台学生所有的评分选项数组
	private String id;

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getSelectArr() {
		return selectArr;
	}

	public void setSelectArr(String selectArr) {
		this.selectArr = selectArr;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public String getTid() {
		return tid;
	}

	public void setTid(String tid) {
		this.tid = tid;
	}

	public String getSid() {
		return sid;
	}

	public void setSid(String sid) {
		this.sid = sid;
	}

	public AppraiseTcherService getAppraisetcherService() {
		return appraisetcherService;
	}

	public void setAppraisetcherService(AppraiseTcherService appraisetcherService) {
		this.appraisetcherService = appraisetcherService;
	}

	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}

	public String openpage() {
		Result<Teacher> Userresult = new Result<Teacher>();

		Userresult.setSuccess(true);
		Userresult.setUrl("AppraiseTcher");

		result = JsonHelper.getJsonHelper().format(Userresult);

		return "json";
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	public String loadAppraiseList() {
		Result<Appraise> Userresult = new Result<Appraise>();

		User user = (User) ActionContext.getContext().getSession().get("User");
		Student stu = user.getStu();

		List list = appraisetcherService.loadlist(stu);
		Userresult.setItems(list);
		// 查询评教是否开启
		Userresult.setSuccess(appraisetcherService.isOpen());

		result = JsonHelper.getJsonHelper().format(Userresult);

		return "json";
	}

	public String openappraisetable() {
		Result<Item> Userresult = new Result<Item>();

		Userresult.setUrl("appraisetable");

		Userresult.setItems(appraisetcherService.initItem());

		result = JsonHelper.getJsonHelper().format(Userresult);

		return "json";
	}

	// 将成绩和评价存入数据库方法
	public String save() {
		Result<Appraise> Userresult = new Result<Appraise>();

		String str = SelectUtils.saveSelect(selectArr, appraisetcherService.initItem());
		double sum = SelectUtils.caculateScore(selectArr, appraisetcherService.initItem());

		appraisetcherService.saveform(id, content, String.valueOf(sum), str);

		Userresult.setMsg(String.valueOf(sum));
		Userresult.setUrl("AppraiseTcher");

		result = JsonHelper.getJsonHelper().format(Userresult);

		return "json";
	}

	public String edit() {
		Result<Appraise> Userresult = new Result<Appraise>();

		String str = SelectUtils.saveSelect(selectArr, appraisetcherService.initItem());
		double sum = SelectUtils.caculateScore(selectArr, appraisetcherService.initItem());
		appraisetcherService.saveform(id, content, String.valueOf(sum), str);

		Userresult.setMsg(String.valueOf(sum));
		Userresult.setUrl("AppraiseList");

		result = JsonHelper.getJsonHelper().format(Userresult);

		return "json";
	}
}